package com.colin.app.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.colin.app.R;
import com.colin.library.help.LogHelp;
import com.colin.library.view.SlidingButtonView;
import com.colin.library.view.TextViewMore;
import com.colin.library.view.help.MetricsHelp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 */

public class TextMoreAdapter extends RecyclerView.Adapter<TextMoreAdapter.ViewHolder> implements SlidingButtonView.IonSlidingButtonListener {
    private Context mContext;
    private IonSlidingViewClickListener mIDeleteBtnClickListener;
    private List<String> mStringList;
    private SlidingButtonView mMenu = null;
    private SparseBooleanArray mSparseBooleanArray = new SparseBooleanArray();
    private int textMoreWidth;

    public TextMoreAdapter(Context context) {
        mContext = context;
        if (null == mStringList) {
            mStringList = new ArrayList<>();
        }
        for (int i = 0; i < 22; i++) {
            mStringList.add(context.getString(R.string.edit_default));
        }
        mIDeleteBtnClickListener = (IonSlidingViewClickListener) context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_text_more, parent, false));
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final String content = mStringList.get(position);
        holder.text_item_more.setText(content);
        //设置内容布局的宽为屏幕宽度
        holder.relative_item_delete_content.getLayoutParams().width = MetricsHelp.getWindowsWidth(mContext);

        ((SlidingButtonView) holder.itemView).setSlidingButtonListener(TextMoreAdapter.this);
        //初始化文本内容
        holder.text_item_more.post(new Runnable() {
            @Override
            public void run() {
                boolean expandState = mSparseBooleanArray.get(position, false);
                holder.text_item_more.updateForRecyclerView(content, textMoreWidth, expandState ? 1 : 0);//第一次getview时肯定为etvWidth为0
                holder.text_recycler_delete.setHeight(holder.relative_item_delete_content.getLayoutParams().height);
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否有删除菜单打开
                if (menuIsOpen()) {
                    closeMenu();//关闭菜单
                } else {
                    int n = holder.getLayoutPosition();
                    mIDeleteBtnClickListener.onItemClick(v, n);
                }
            }
        });
        holder.text_item_more.setOnMoreListener(new TextViewMore.OnMoreListener() {
            @Override
            public void onExpand(TextViewMore view) {
                mSparseBooleanArray.put(position, view.getExpandState() == 1);
            }

            @Override
            public void onShrink(TextViewMore view) {
                mSparseBooleanArray.put(position, view.getExpandState() == 1);
            }
        });

        holder.text_recycler_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int n = holder.getLayoutPosition();
                mIDeleteBtnClickListener.onDeleteBtnCilck(v, n);
            }
        });

    }

    @Override
    public int getItemCount() {
        return null == mStringList ? 0 : mStringList.size();
    }

    public void removeData(int position) {
        mStringList.remove(position);
        notifyItemRemoved(position);

    }

    /**
     * 删除菜单打开信息接收
     */
    @Override
    public void onMenuIsOpen(View view) {
        mMenu = (SlidingButtonView) view;
    }

    /**
     * 滑动或者点击了Item监听
     *
     * @param slidingButtonView
     */
    @Override
    public void onDownOrMove(SlidingButtonView slidingButtonView) {
        if (menuIsOpen()) {
            if (mMenu != slidingButtonView) {
                closeMenu();
            }
        }
    }

    /**
     * 关闭菜单
     */
    public void closeMenu() {
        mMenu.closeMenu();
        mMenu = null;

    }

    /**
     * 判断是否有菜单打开
     */
    public Boolean menuIsOpen() {
        if (mMenu != null) {
            return true;
        }
        LogHelp.e("asd", "mMenu为null");
        return false;
    }

    public interface IonSlidingViewClickListener {
        void onItemClick(View view, int position);

        void onDeleteBtnCilck(View view, int position);
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView text_recycler_delete;
        public final TextViewMore text_item_more;
        public final RelativeLayout relative_item_delete_content;

        public ViewHolder(View itemView) {
            super(itemView);
            ((SlidingButtonView) itemView).setSlidingButtonListener(TextMoreAdapter.this);
            text_recycler_delete = (TextView) itemView.findViewById(R.id.text_recycler_delete);
            text_item_more = (TextViewMore) itemView.findViewById(R.id.text_item_more);
            relative_item_delete_content = (RelativeLayout) itemView.findViewById(R.id.relative_item_delete_content);
        }
    }
}
